
//#include "head.h"
//#include <stdio.h>
#include <signal.h>

#include <iostream>

#include <workflow/WFFacilities.h>
#include <workflow/WFTaskFactory.h>
#include <workflow/HttpMessage.h>
#include <workflow/HttpUtil.h>
using std::cout;
using std::endl;
using std::vector;
using std::string;

//
//初始化计数器的值为1
static WFFacilities::WaitGroup waitGroup(1);

void sighandler(int num)
{
    printf(">> sig num %d is comming\n", num);
    //调用一次done方法，计数器的值减1
    waitGroup.done();
}

void parallelCallback(const ParallelWork * )
{   printf("parallelCallback is running.\n");   }

void parallelSeriesCallback(const SeriesWork *)
{   printf("parallelSeriesCallback is running.\n"); }

void outsideSeriesCallback(const SeriesWork *) 
{   printf("outsideSeriesCallback is running.\n");  }

void httpCallback(WFHttpTask * httptask)
{   
    printf("httpCallback is running.\n");   
    //1. 对任务的状态进行检测
    int state = httptask->get_state();
    int error = httptask->get_error();
    switch(state) {
    case WFT_STATE_SYS_ERROR:
        printf("system error: %s\n", strerror(error)); break;
    case WFT_STATE_DNS_ERROR:
        printf("dns error: %s\n", gai_strerror(error)); break;
    case WFT_STATE_SUCCESS:
        break;
    }
    if(state != WFT_STATE_SUCCESS) {
        printf("error occurs!\n");
        return;
    }
    //2. 对HttpRequest进行遍历
    printf("task is ok!\n");
    auto req = httptask->get_req();
    //请求报文的起始行
    printf("%s %s %s\n", req->get_method(),
            req->get_request_uri(),
            req->get_http_version());
    //请求报文的首部字段, 借助于迭代器来访问
    protocol::HttpHeaderCursor cursor(req);
    string name, value;
    while(cursor.next(name, value)) {
        cout << name << ": " << value << endl;
    }
    printf("\r\n\r\n");
    //3. 对HttpResponse进行遍历
    auto resp = httptask->get_resp();
    //响应报文的起始行
    printf("%s %s %s\n", resp->get_http_version(),
            resp->get_status_code(),
            resp->get_reason_phrase());

    //响应报文的首部字段, 借助于迭代器来访问
    protocol::HttpHeaderCursor cursor2(resp);
    while(cursor2.next(name, value)) {
        cout << name << ": " << value << endl;
    }
    printf("-------------------------------------\n\n");

}

void test0()
{
    signal(SIGINT, sighandler);

    vector<string> webs{
        "https://www.baidu.com",
        "https://www.zhihu.com",
        "https://www.sina.com"
    };

    auto parallelWork = Workflow::create_parallel_work(parallelCallback);
    for(size_t i = 0; i < webs.size(); ++i) {
        auto httpTask = WFTaskFactory::create_http_task(webs[i], 1, 1, httpCallback);
        //设置任务的属性
        httpTask->get_req()->set_header_pair("User-Agent", "Workflow client");

        auto series = Workflow::create_series_work(httpTask, parallelSeriesCallback);
        parallelWork->add_series(series);
    }

    //方案一：启动并行任务
    //parallelWork->start();//
    
    //方案二: 创建序列，将parallelWork放入序列中，同时注册序列的回调函数
    auto seriesOfParallelWork = Workflow::create_series_work(parallelWork, outsideSeriesCallback);
    seriesOfParallelWork->start();


    //当调用wait方法会阻塞当前线程
    waitGroup.wait();
    cout << "test0 exit" << endl;
}


int main()
{
    test0();
    return 0;
}

